'use strict';

const koa = require('koa-router')();
const router = require('koa-router')();
const fs = require('fs');
const path = require('path');
const util = require('util');
const cfgUtil = require('./../server/commons/factories/configuration');

const serverCfg = cfgUtil.get('server');
const ROUTER = serverCfg.router;

Object.keys(ROUTER).forEach((key) => {
    let obj = ROUTER[key];
    let dir = obj.dir;
    let exclude = obj.exclude;
    (fs.readdirSync(dir) || []).forEach(function(file) {
        let fileNameParser = path.parse(file);
        if (fileNameParser.ext === '.js') {
            if (exclude.indexOf(fileNameParser.name) === -1) {
                let fn = require(path.join(dir, file));
                if(util.isFunction(fn)) {
                    fn(router);
                }
            }
        }
    });
});


koa.use('/', router.routes(), router.allowedMethods());

module.exports = koa;